package product;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import net.miginfocom.swing.MigLayout;


public class CustomerInput extends JInternalFrame {
	Map<String, CustomerData> customer = new HashMap<>();
	boolean flag = false;
	
	Image img;			// 로고 이미지 저장
	String fileName;	// 이미지의 로컬경로 저장
	File f;				
	String folder = "/logoImg/";	// 이미지를 저장할 경로
	
	DecimalFormat df = new DecimalFormat("###-##-######");
	
	private JLabel lblNewLabel;
	private JLayeredPane layeredPane;
	private JLabel lblNewLabel_1;
	private JTextField inputName;
	private JLabel lblNewLabel_2;
	private JTextField inputCEO;
	private JLabel lblNewLabel_3;
	private JTextField inputPhone;
	private JLabel lblNewLabel_4;
	private JTextField inputAddress;
	private JLabel lblLogo;
	private JButton btnLogo;
	private JButton btnCodeSearch;
	private JSeparator separator;
	private JSeparator separator_1;
	private JButton btnSave;
	private JButton btnCancel;
	private JButton btnReset;
	private JFormattedTextField inputCode;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					CustomerInput frame = new CustomerInput();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CustomerInput() {
		super("거래처 추가", true, true, true, false);
		setBounds(0, 0, 466, 339);
		getContentPane().setLayout(new MigLayout("", "[90px][12px][90px][12px][206px]", "[212px][40px]"));
		getContentPane().setLayout(new MigLayout("", "[1px]", "[1px]"));
		getContentPane().add(getLayeredPane_1(), "cell 0 0,alignx center,aligny center");
		getContentPane().add(getBtnSave(), "cell 0 0,grow");
		getContentPane().add(getBtnCancel(), "cell 0 0,grow");
		getContentPane().add(getBtnReset(), "cell 0 0,grow");

	}
	
	public void setC(Map c){
		this.customer = c;
	}
	
	public void logoLoad(){
		try{
			JFileChooser fc = new JFileChooser();
			int yn = fc.showOpenDialog(this);
			if (yn == JFileChooser.APPROVE_OPTION) {
				File f = fc.getSelectedFile();
				fileName = f.getPath();

				lblLogo.setIcon(new ImageIcon(fileName));
				lblLogo.setText("");
			}
		}catch(Exception ex){ex.getMessage();}
	}
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("사업자번호*");
			lblNewLabel.setPreferredSize(new Dimension(70, 20));
		}
		return lblNewLabel;
	}
	private JLayeredPane getLayeredPane_1() {
		if (layeredPane == null) {
			layeredPane = new JLayeredPane();
			layeredPane.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
			layeredPane.setLayout(new MigLayout("", "[80.00px][129.00px,grow][55.00][12.00][90.00]", "[28.00][15.00][28][28][15.00][28][28.00]"));
			layeredPane.add(getLblNewLabel(), "cell 0 0,grow");
			layeredPane.add(getInputCode(), "cell 1 0");
			layeredPane.add(getBtnCodeSearch(), "cell 2 0,alignx center,growy");
			layeredPane.add(getLblLogo(), "cell 4 0 1 3,grow");
			layeredPane.add(getSeparator(), "cell 0 1 3 1,growx");
			layeredPane.add(getLblNewLabel_1(), "cell 0 2,grow");
			layeredPane.add(getInputName(), "cell 1 2 2 1,growx,aligny center");
			layeredPane.add(getLblNewLabel_2(), "cell 0 3,alignx left,growy");
			layeredPane.add(getInputCEO(), "cell 1 3 2 1,growx,aligny center");
			layeredPane.add(getBtnLogo(), "cell 4 3,grow");
			layeredPane.add(getSeparator_1(), "cell 0 4 5 1,growx");
			layeredPane.add(getLblNewLabel_3(), "cell 0 5,alignx left,growy");
			layeredPane.add(getInputPhone(), "cell 1 5 4 1,growx,aligny center");
			layeredPane.add(getLblNewLabel_4(), "cell 0 6,alignx left,growy");
			layeredPane.add(getInputAddress(), "cell 1 6 4 1,growx,aligny center");
		}
		return layeredPane;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("회사명*");
		}
		return lblNewLabel_1;
	}
	private JTextField getInputName() {
		if (inputName == null) {
			inputName = new JTextField();
			inputName.setColumns(10);
		}
		return inputName;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("대표자명*");
		}
		return lblNewLabel_2;
	}
	private JTextField getInputCEO() {
		if (inputCEO == null) {
			inputCEO = new JTextField();
			inputCEO.setColumns(10);
		}
		return inputCEO;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("전화번호");
		}
		return lblNewLabel_3;
	}
	private JTextField getInputPhone() {
		if (inputPhone == null) {
			inputPhone = new JTextField();
			inputPhone.setColumns(10);
		}
		return inputPhone;
	}
	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("주소");
		}
		return lblNewLabel_4;
	}
	private JTextField getInputAddress() {
		if (inputAddress == null) {
			inputAddress = new JTextField();
			inputAddress.setColumns(10);
		}
		return inputAddress;
	}
	private JLabel getLblLogo() {
		if (lblLogo == null) {
			lblLogo = new JLabel("회사로고");
			lblLogo.setHorizontalAlignment(SwingConstants.CENTER);
			lblLogo.setBorder(new LineBorder(new Color(0, 0, 0)));
		}
		return lblLogo;
	}
	private JButton getBtnLogo() {
		if (btnLogo == null) {
			btnLogo = new JButton("파일 선택");
			btnLogo.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					logoLoad();
					
				}
			});
		}
		return btnLogo;
	}
	private JButton getBtnCodeSearch() {
		if (btnCodeSearch == null) {
			btnCodeSearch = new JButton("조회");
			btnCodeSearch.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					Iterator<String> it = customer.keySet().iterator();
					flag = false;
					while(it.hasNext()){
						if(inputCode.getText().equals(it.next())){
							flag = true;
							break;
						}
					}
					if(flag){
						JOptionPane.showMessageDialog(null, "이미 등록된 사업자번호입니다.");
						inputCode.setText("");
					}else{
						JOptionPane.showMessageDialog(null, "사용 가능한 사업자번호입니다.");
						flag = true;
					}
				}
			});
		}
		return btnCodeSearch;
	}
	private JSeparator getSeparator() {
		if (separator == null) {
			separator = new JSeparator();
			separator.setBackground(new Color(255, 255, 255));
			separator.setOpaque(true);
			separator.setForeground(new Color(0, 0, 0));
			separator.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		}
		return separator;
	}
	private JSeparator getSeparator_1() {
		if (separator_1 == null) {
			separator_1 = new JSeparator();
			separator_1.setOpaque(true);
			separator_1.setForeground(Color.BLACK);
			separator_1.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
			separator_1.setBackground(Color.WHITE);
		}
		return separator_1;
	}
	private JButton getBtnSave() {
		if (btnSave == null) {
			btnSave = new JButton("저장");
			btnSave.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if(inputCode.getText().equals("")){
						JOptionPane.showMessageDialog(null, "사업자번호는 필수항목입니다.");
						inputCode.setRequestFocusEnabled(true);
						
					}else if(!flag){
						JOptionPane.showMessageDialog(null, "먼저 사업자번호를 조회해 주세요.");
						
					}else if(inputName.getText().equals("")){
						JOptionPane.showMessageDialog(null, "회사명은 필수항목입니다.");
						inputName.setRequestFocusEnabled(true);
						
					}else if(inputCEO.getText().equals("")){
						JOptionPane.showMessageDialog(null, "대표자명은 필수항목입니다.");
						inputCEO.setRequestFocusEnabled(true);
						
					}else{
						CustomerData cd = new CustomerData(inputCode.getText(), inputName.getText(), 
								inputCEO.getText(), inputPhone.getText(), inputAddress.getText());
						customer.put(inputCode.getText(), cd);
						
						try{
							File f = new File(folder);
							if(!f.exists()){
								f.mkdirs();
							}
				
							BufferedImage img = ImageIO.read(new File(fileName));
							ImageIO.write(img, "png", new File(folder + inputCode.getText() + ".png")); 							
							
						}catch(Exception ex){ex.getMessage();}
						
						JOptionPane.showMessageDialog(null, "자~~알 저장되었습니다.");
						
						inputCode.setText("");
						inputName.setText("");
						inputCEO.setText("");
						inputAddress.setText("");
						inputPhone.setText("");
						lblLogo.setIcon(new ImageIcon());
						lblLogo.setText("회사로고");
					}
				}
			});
		}
		return btnSave;
	}
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton("취소");
			btnCancel.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					int yn = JOptionPane.showConfirmDialog(null, "창을 닫으시겠습니까??");
					if(yn == JOptionPane.OK_OPTION){
						inputCode.setText("");
						inputName.setText("");
						inputCEO.setText("");
						inputAddress.setText("");
						inputPhone.setText("");
						setVisible(false);
					}
				}
			});
		}
		return btnCancel;
	}
	private JButton getBtnReset() {
		if (btnReset == null) {
			btnReset = new JButton("다시작성");
			btnReset.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					int yn = JOptionPane.showConfirmDialog(null, "다시 작성 하시겠습니까??");
					if(yn == JOptionPane.OK_OPTION){
						inputCode.setText("");
						inputName.setText("");
						inputCEO.setText("");
						inputAddress.setText("");
						inputPhone.setText("");
					}
				}
			});
		}
		return btnReset;
	}
	private JFormattedTextField getInputCode() {
		if (inputCode == null) {
			inputCode = new JFormattedTextField();
			try {
		        inputCode.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("###-##-#####")));
		    } catch(ParseException e) {
		        e.printStackTrace();
		    }
			inputCode.setColumns(12);
		}
		return inputCode;
	}
}
